<?php

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once('../services/webservices.php');  
require_once("index.php");
?>
<br />
<br /> 
<br /> 
       

<html>
<title>Update Category</title>
 <link href="../includes/admin.css " rel="stylesheet" type="text/css" />
<body>
<div class="leftfloatbar">  
<td align=right>
<tr>
              
<b>Choose a Category:</b>
<form action="updatecategory.php" method="GET" id="updatecategoryform" name="updatecategoryform">
<select name="category_id">
<option value="0">Select a Category...</option>
<?php
 


$getRequest = new itemcategory();
$getResponse = $getRequest->getallitemcategories();

//Loop through returned XML and display all items        
$xml = new SimpleXMLElement($getResponse);

$Item_Count = $xml->Item_Count;

if($Item_Count > 0)
    {
       
    foreach ($xml->Item as $eachitem)         
        {
        $category_id = $eachitem->category_id;
        $category = $eachitem->category;            
?>
<option value="<?php echo $category_id;?>" onclick="document.updatecategoryform.submit();"><?php echo $category;?></option>
<?php
        }
    }
    else
    {
?>
<option value="0">No Categories Found.</option>
<?php
    }        
?>
</select>
</form>
</tr>
</td>
</div>
<br />
<br /> 
<br /> 
<br /> 
<br /> 

  
<?php




if (isset($_GET['category_id']))
{
    $category_id = $_GET['category_id'];
    
    $getrequest = new itemcategory();             
    $getrequest->category_id = $category_id;
    $getresponse = $getrequest->getcategorybycategoryid();
    
    $xml = new SimpleXMLElement($getresponse);
    
    $item_count = $xml->Item_Count;
    
    if ($item_count > 0)
    {
        foreach ($xml->Item as $eachitem)
        {
            
            $category_id = $eachitem->category_id;
            $category = $eachitem->category;
            $parent_category_id = $eachitem->parent_category_id;
            $description = $eachitem->description;
           
            ?> 
            <div class="doubleborderrightmargin">
            <td align=right valign=top>
            <?php
            //this is to get the name that corresponds to the parent_category_id
            $getrequest2 = new itemcategory();
            $getrequest2->category_id = $parent_category_id;
            $getresponse2 = $getrequest2->getcategorybycategoryid();
            $xml2 = new SimpleXMLElement($getresponse2);
            foreach($xml2->Item as $item)
            {
                $parent_category_name = $item->category;
               
            }
            
                
            
            
            
            
            ?>
            <tr> 
            <form action="updatecategory.php" method="POST" id="updatecategory" name="updatecategory">
          
           <b>Select Parent Category</b>
           <select name="parent_category_id">
           
          
           <?php
           if($parent_category_id!=0)
           {
               echo' <option value="' . $parent_category_id . '">' . $parent_category_name . '</option>';
           }
            echo '<option value="0">No Parent Category</option>';     
           $getRequest = new itemcategory();
           $getResponse = $getRequest->getallitemcategories();
           //Loop through returned XML and display all items        
           $xml = new SimpleXMLElement($getResponse);

           $Item_Count = $xml->Item_Count;
           if($Item_Count > 0)
           {
       
                foreach ($xml->Item as $eachitem)         
                {
                    $select_category_id = $eachitem->category_id;
                    $select_category = $eachitem->category;            
                    ?>
                    <option value="<?php echo $select_category_id;?>"><?php echo $select_category;?></option>
                    <?php
                }
           }
           else
           {
           ?>
                <option value="0">No Categories Found.</option>
           <?php
           }        
           ?>
           </select>
            
            
            
            <input type="hidden" name="category_id" id="category_id" value="<?php echo $category_id ?>">         
            <br />
            <br />
            <b>Category Name:</b>
            <input type="text" name="category" id="category" value ="<?php echo $category; ?>" size="<?php echo strlen($category);?>">
            <br />
            <br />
            <b>Category Description:</b>
            <input type="text" name="description" id="description" value="<?php echo $description; ?>" size="<?php echo strlen($description); ?>">
            <br />
            
             <br />
            <input type="submit" name="submit" value="Update Category">
            <br />
            <br />
            <input type="submit" name="delete" value="Delete Category"  onclick="javascript: return confirm('Deleting this category will delete this category and all subcategories of this category.  All items within those categories will also be deleted. Proceed with caution.');"> 
            </tr> 
            </td>
            </div>      
        <?php
        }
    }
     
}

if (isset($_POST['submit']))
{
   $category_id = $_POST['category_id'];  
   $category = $_POST['category'];
   $description = $_POST['description'];
   $parent_category_id = $_POST['parent_category_id'];
   if($parent_category_id == 0)
   {
       $parent_category_id = "";
   }

   $getRequest = new itemcategory();
   $getRequest->category_id = $category_id;
   $getRequest->category = $category;
   $getRequest->description = $description;
   $getRequest->parent_category_id = $parent_category_id;
   $getresponse = $getRequest->updateitemcategory();
   $xml = new SimpleXMLElement($getresponse);
  
  echo '    
        <script type="text/javascript">
        <!--
        window.location = "./updatecategory.php"
        //-->                                                                                                        
        </script> 
        '; 
       
}
                         
if (isset($_POST['delete']))
{
   $category_id = $_POST['category_id'];  
  
   
  
   
   $getRequest = new itemcategory();
   $getRequest->category_id = $category_id;
   $getResponse = $getRequest->deleteitemcategory();
  
   echo '    
        <script type="text/javascript">
        <!--
        window.location = "./updatecategory.php"
        window.location = "./updatecategory.php"
        //-->                                                                                                        
        </script> 
        '; 
  
  
    
}
       
   ?>
 <br />  
 <br />
 <br />
 <br />
 <br />
 


</body>
</html>
 
    
    
    
    
    